<template>
    <div class="app_container">
        <el-card >
            <div slot="header" class="clearfix">
                <el-button type="primary" @click="handleAdd">&emsp;新增&emsp;</el-button>
            </div>
            <div class="item" v-loading="listLoading">
                <el-table ref="dataList" :data="menuList" style="width:100%;margin-bottom: 20px;" row-key="id" border :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                    <el-table-column prop="id" label="ID"> </el-table-column>
                    <el-table-column prop="name" label="名称"> </el-table-column>
                    <el-table-column prop="sort" label="排序" > </el-table-column>

                    <el-table-column label="操作" width="180px">
                        <template slot-scope="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)" plain>编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页 -->
                <!-- <cus-pagination
                    :total="total"
                    :page.sync="listQuery.page"
                    :limit.sync="listQuery.perPage"
                    @pagination="getList"
                /> -->
            </div>
        </el-card>

        <!-- 删除弹框 -->
        <el-dialog
            title="确定删除吗?"
            :visible.sync="deleteDialogVisible"
            width="435px"
            >
            <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog :visible.sync="dialogFormVisible" :title="titleMap[dialogStatus]" @close="handleDialogClose('editRulesForm')">
            <el-form :model="editRulesForm" :rules="editRulesFormRules" ref="editRulesForm" label-width="120px" class="demo-ruleForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="editRulesForm.name" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="分钟数" prop="total_minute">
                    <el-input v-model="editRulesForm.total_minute" placeholder="请输入名称"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="editRulesForm.sort" placeholder="请输入排序"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="dialogFormVisible = false">返回</el-button>
                    <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
                </el-form-item>
                </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    listData,
    postData,
    deleteData
} from "@/api/auctionRoom/auctionTime"

export default {
    data(){
        return {
            confirmBtn:false,

            dataIds:undefined,
            deleteDialogVisible:false,

            dialogStatus: '',
            // dialog的头部信息
            titleMap: {
                update: '编辑',
                create: '添加'
            },
            editRulesFormRules:{
                name:[{required:true,message:'请输入名称',trigger:'blur'}],
                total_minute:[{required:true,message:'请输入分钟数',trigger:'blur'}],
                sort:[{required:true,message:'请输入排序',trigger:'blur'}]
            },
            dialogFormVisible:false,
            editRulesForm:{
                id:undefined,
                name:undefined,
                total_minute:undefined,
                sort:undefined
            },
            menuList:[],
            listQuery:{
                page:1,
                perPage:10,
            },
            total:0,
        }
    },
    created(){
        this.getList()
    },
    methods:{
        getList(){
            this.listLoading = true;
            let urlData = "?";
            if (this.listQuery.page) {
                urlData = urlData + `page=${this.listQuery.page}&`;
            }
            if (this.listQuery.perPage) {
                urlData = urlData + `perPage=${this.listQuery.perPage}`;
            }
            listData(urlData).then((response) => {
                let arr = response.data
                arr.forEach((item) => {
                    for(let key in item){
                        if(key === 'add_time'){
                            item.add_time = this.getData(item.add_time)
                        }
                    }
                })

                this.menuList = arr;

                // this.total = response.data.meta.total;
                this.listLoading = false

                this.confirmBtn = false
            })
        },
        getData(value){
            let temp = new Date(value*1000)
            let y = temp.getFullYear();
            let m = temp.getMonth()+1;
            let d = temp.getDate();
            let h = temp.getHours();
            let mm = temp.getMinutes();
            let s = temp.getSeconds();
            return y+'-'+this.add0(m)+'-'+this.add0(d)+' '+this.add0(h)+':'+this.add0(mm)+':'+this.add0(s);
        },
        add0(m){
            return m<10?'0'+m:m
        },
        handleAdd(){
            this.resetForm('editRulesForm')
            this.dialogStatus = "create"
            this.dialogFormVisible = true
        },
        handleEdit(row){
            this.editRulesForm.id = row.id
            this.editRulesForm.name = row.name;
            this.editRulesForm.total_minute = row.total_minute;

            this.editRulesForm.sort = row.sort

            this.dialogStatus = 'update'
            this.dialogFormVisible = true
        },
        resetForm(formName) {
            this.$nextTick(()=>{
                this.$refs[formName].resetFields();
            })
            this.editRulesForm = {
                id:undefined,
                name:undefined,
                sort:undefined
            }
        },
        submitForm(formName) {
            if(this.confirmBtn) return
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.confirmBtn = true
                    if(this.dialogStatus === 'create'){
                        let formData = new FormData()
                        for(let item in this.editRulesForm){this.editRulesForm[item] === undefined? '':formData.append(item, this.editRulesForm[item]);}

                        postData(formData).then(response => {
                            if (response.code === 200) {
                                this.getList()
                                this.dialogFormVisible = false;
                                this.$notify({
                                    type:'success',
                                    message: '操作成功',
                                    position: 'bottom-right',
                                    showClose: false,
                                    duration: 3000
                                });
                            } else {
                                this.confirmBtn = false
                                this.$message({
                                    message: response.message,
                                    type: 'error'
                                });
                            }
                        })
                    }else {
                        let formData = new FormData()
                        for(let item in this.editRulesForm){this.editRulesForm[item] === undefined? '':formData.append(item, this.editRulesForm[item]);}
                        postData(formData).then(response => {
                            if (response.code === 200) {
                                this.getList()
                                this.dialogFormVisible = false;
                                this.$notify({
                                    type:'success',
                                    message: '操作成功',
                                    position: 'bottom-right',
                                    showClose: false,
                                    duration: 3000
                                });
                            } else {
                                this.confirmBtn = false
                                this.$message({
                                    message: response.message,
                                    type: 'error'
                                });
                            }
                        })
                    }
                } else {

                    return false;
                }
            });
        },
        // 点击单个删除
        handleDelete(row) {
            this.dataIds = row.id
            this.deleteDialogVisible = true
        },
        // 确定删除
        confirmDelete(){
            if(this.confirmBtn) return
            this.confirmBtn = true
            // let formData = new FormData()
            // formData.append('id',this.dataIds)
            let obj = {id:this.dataIds}
            deleteData(obj).then((response) => {
                if (response.code === 200) {
                    this.getList()
                    this.deleteDialogVisible = false;
                    this.$notify({
                        type:'success',
                        message: '操作成功',
                        position: 'bottom-right',
                        showClose: false,
                        duration: 3000
                    });
                } else {
                    this.confirmBtn = false
                    this.$message({
                        message: response.message,
                        type: 'error'
                    });
                }
            })
        },
        // 监听dialog关闭时的处理事件
        handleDialogClose(formName) {
            if (this.$refs[formName]) {
                this.$refs[formName].clearValidate(); // 清除整个表单的校验
            }
        },
    }
}
</script>

<style>

</style>
